package cn.zn.modules.budget.his.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.zn.modules.budget.his.entity.BudgetInfoSum;
import cn.zn.modules.budget.his.entity.QBudgetInfoSum;
import cn.zn.modules.budget.his.entity.dto.BudgetInfoSumDTO;
import cn.zn.modules.budget.his.repository.BudgetInfoSumRepository;
import cn.zn.modules.budget.his.service.BudgetInfoSumService;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("budgetInfoSumService")
public class BudgetInfoSumServiceImpl implements BudgetInfoSumService {

    @Autowired
    private BudgetInfoSumRepository budgetInfoSumRepository;

    @Override
    public Iterable<BudgetInfoSum> findSum(BudgetInfoSumDTO budgetInfoSumDTO) {
        QBudgetInfoSum qBudgetInfoSum = QBudgetInfoSum.budgetInfoSum;
        Predicate predicate = qBudgetInfoSum.isNotNull().or(qBudgetInfoSum.isNull());
        if (budgetInfoSumDTO.getAp() != null) {
            predicate = ExpressionUtils.and(predicate, qBudgetInfoSum.ap.eq(budgetInfoSumDTO.getAp()));
        }
        if(StrUtil.isNotEmpty(budgetInfoSumDTO.getDeptCode())){
            predicate = ExpressionUtils.and(predicate, qBudgetInfoSum.deptCode.eq(budgetInfoSumDTO.getDeptCode()));
        }
        if (StrUtil.isNotBlank(budgetInfoSumDTO.getProjectName())) {
            predicate = ExpressionUtils.and(predicate, qBudgetInfoSum.projectName.like("%" + budgetInfoSumDTO.getProjectName().trim() + "%"));
        }
        if (StrUtil.isNotBlank(budgetInfoSumDTO.getCateName())) {
            predicate = ExpressionUtils.and(predicate, qBudgetInfoSum.cateName.like("%" + budgetInfoSumDTO.getCateName().trim() + "%"));
        }
        Iterable<BudgetInfoSum> budgetInfoSums = budgetInfoSumRepository.findAll(predicate);
        return budgetInfoSums;
    }
}